#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>

int main() {

    pid_t id = fork();
    if (id == 0) {
        int count = 3;
        while (count) {
            printf("child is runnig : %d, ppid : %d, pid : %d\n", count, getppid(), getpid());
            count--;
            printf("*************count : %d**************\n", count);
            sleep(1);
            //int *p = 123234;
            //*p = 100;
        }
        printf("child quit...\n");
        exit(123);
    }
    int status = 0;
    //pid_t ret = waitpid(id, &status, 0);
    while (1) {
        //sleep(1);
        pid_t ret = waitpid(-1, &status, WNOHANG);
        //printf("----------- ret : %d --------------\n", ret);
        if (ret == 0) {
     //       printf("wait next !\n");
     //       printf("father do other thing !\n");
            //sleep(1);
        } else if (ret > 0) {
            printf("wait success, ret : %d, code : %d\n", ret, WEXITSTATUS(status));
            break;
        } else {
            printf("wait failed !\n");
            break;
        }
    }
   // int code = (status >> 8) & 0xff;
   // int sig = status & 0x7f;
   // printf("father wait done, ret : %d, exit code : %d, sig code : %d\n", ret, code, sig);
    return 0;
    
    
    
    
    
    
//    pid_t id = fork();
//    if (id < 0) {
//        perror("fork");
//        return 1;
//    }
//    if (id == 0) {
//        int count = 5;
//        while (count) {
//            printf("child(pid is : %d, ppid is : %d) is running: %d\n", getpid(), getppid(), count--);
//            sleep(1);
//        }
//        printf("child quit...\n");
//        exit(0);
//    } else {
//        printf("father is waiting...\n");
//        sleep(10);
//        pid_t ret = wait(NULL);
//        sleep(3);
//        printf("father is wait done, ret : %d\n", ret);
//    }
//    return 0;
}
